using System;
using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

public class CommentSocket : MonoBehaviour
{
    private ReceiveSocket receiveSocket;
    private StudentManager studentManager;

    void Start()
    {
        // 获取ReceiveSocket实例
        receiveSocket = FindObjectOfType<ReceiveSocket>();
        studentManager = FindObjectOfType<StudentManager>();
        if (receiveSocket == null)
        {
            Debug.LogError("未找到ReceiveSocket实例");
            return;
        }
        if (studentManager == null)
        {
            Debug.LogError("未找到StudentManager实例");
            return;
        }
        // 订阅评论接收事件
        if (receiveSocket != null)
        {
            receiveSocket.OnLiveCommentReceived += HandleComment;
            receiveSocket.OnJoinCampReceived += HandleJoinCamp;
        }
    }

    void OnDestroy()
    {
        // 取消订阅
        if (receiveSocket != null)
        {
            receiveSocket.OnLiveCommentReceived -= HandleComment;
            receiveSocket.OnJoinCampReceived -= HandleJoinCamp;
        }
    }

    private void HandleComment(LiveCommentData comment)
    {
        // 去除前后空格并转为大写
        string content = comment.content?.Trim().ToUpper();
        // 先检查用户信息是否存在
        if (comment.game_user == null)
        {
            Debug.LogWarning($"用户{comment.nickname}没有game_user信息");
            return;
        }

        // 获取用户等级
        int userLevel = comment.game_user.level;
        Debug.Log($"用户等级: {userLevel}");
        int userId = comment.game_user.id;
        //通过ContainsStudent方法判断是否已经进场
        if (!studentManager.ContainsStudent(userId.ToString())){
            switch (content)
            {
                case "1":
                    Debug.Log($"等级{userLevel}用户选择了选项1");
                    studentManager.UserEnter(comment,1);
                    break;
                case "2":
                    Debug.Log($"等级{userLevel}用户选择了选项2");
                    studentManager.UserEnter(comment,2);
                    break;
                default:
                    Debug.Log($"等级{userLevel}输入随便评论进场");
                    studentManager.UserEnter(comment,0);
                    break;
            }
        }
        
        // 启动协程处理答题逻辑
        StartCoroutine(HandleAnswerLogic(comment, content, userLevel, userId));
    }

    private IEnumerator HandleAnswerLogic(LiveCommentData comment, string content, int userLevel, int userId)
    {
        // 等待1秒
        yield return new WaitForSeconds(1f);
        
        //处理答题逻辑
        Student st = studentManager.GetStudentById(userId.ToString())?.GetComponentInChildren<Student>();
        if (st == null)
        {
            Debug.LogWarning($"未找到用户 {userId} 的Student组件");
            yield break;
        }

        switch (content)
        {
            case "A":
                Debug.Log($"等级{userLevel}用户选择了选项A");
                st.setAnswer(1);
                StringEventSystem.Global.Send(EventID.ShowAnswer, new string[] { "A", userId.ToString() });
                break;
            case "B":
                Debug.Log($"等级{userLevel}用户选择了选项B");
                st.setAnswer(2);
                StringEventSystem.Global.Send(EventID.ShowAnswer, new string[] { "B", userId.ToString() });
                break;
            case "C":
                Debug.Log($"等级{userLevel}用户选择了选项C");
                st.setAnswer(3);
                StringEventSystem.Global.Send(EventID.ShowAnswer, new string[] { "C", userId.ToString() });
                break;
            case "D":
                Debug.Log($"等级{userLevel}用户选择了选项D");
                st.setAnswer(4);
                StringEventSystem.Global.Send(EventID.ShowAnswer, new string[] { "D", userId.ToString() });
                break;
            // case var x when x.StartsWith("冠名"):
            //     Debug.Log($"等级{userLevel}用户选择了冠名");
            //     StringEventSystem.Global.Send(EventID.GuanMing,comment);
            //     break;
            default:
                Debug.Log($"等级{userLevel}用户输入了无效选项");
                break;
        }
    }

    private void HandleJoinCamp(JoinCampData campData){
        //获取userid
        int userid = campData.game_user.id; 
        //查找id是否已经存在
        if(studentManager.ContainsStudent(userid.ToString())){
            Debug.Log($"用户{userid}已存在");
            return;
        }
        LiveCommentData liveCommentData = new LiveCommentData();
        liveCommentData.game_user = campData.game_user;
        liveCommentData.nickname = campData.game_user.name;
        liveCommentData.avatar_url = campData.game_user.avatar_url;
        //判断加入的队伍
        switch (campData.group_id)
        {
            case 1: 
                Debug.Log($"小摇杆用户选择队伍1");
                studentManager.UserEnter(liveCommentData,1);
                break;
            case 2: 
                Debug.Log($"小摇杆用户选择队伍2");
                studentManager.UserEnter(liveCommentData,1);
                break;
        }
    }
}
